/**
* @Title: insuredBlacklist_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @author EX-XIEYUWEN001
* @date 2014年10月29日 下午14:46:06
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('InsuredBlackListAuditBackCtrl', ['$scope', '$http','BaseURL', 'Modal','$modalInstance','BaseService','CommunityService', function($scope, $http, baseUrl, Modal,$modalInstance,BaseService
			,CommunityService){

		var opertionType=$scope.opertionType;
		var attentionLevel=null;
		var url= baseUrl + 'integrityBlack/addIntegrityBlack.do'; //默认是保存的url
		if(opertionType=='Add'){
			$scope.person={};
		}else if(opertionType=='Editor'){
			attentionLevel=$scope.person.attentionLevel;
			url= baseUrl + 'integrityBlack/updateIntegrityBlack.do'; //更新的URL地址
		}
		$scope.baseService = BaseService;
		$scope.communityService = CommunityService;
	    CommunityService.getBaseCode('{codeType:\'id_type\'}');
	    
		BaseService.initSexCollection();
		
		
		
		initAttentionIllegal();
		
		
		/**
		 * 关注级别的下拉框改变事件
		 */
		$scope.selectChangeFun=function(){
			var personSelectLevel=$scope.person.attentionLevel;
			if(attentionLevel!=null&&attentionLevel!=$scope.person.attentionLevel){
	            $scope.person.dateStart=null;
	            $scope.person.dateEnd=null;
			}
			if(personSelectLevel){
				$.each($scope.selectAttentionLevelList,function(index,item){
					  var attentionMidId=item.attentionMidId;
					  if(attentionMidId==personSelectLevel){
						  var attentionTime=item.attentionTime;

						  if(attentionTime=='0'){
							  $scope.person.attentionTime="终身";
						  }else{
							  $scope.person.attentionTime=attentionTime+"年";
						  }
						  return false;
					  }
				});
			}
		}
		
		/**
		 * 初始化违规情节集合
		 */
		function initAttentionIllegal(){
			 $http({
					method : 'post',
					cache : false,
					url : baseUrl + 'integrityBlack/initAttentionLevelList.do',
					params :{}
				}).success(function(response, status, headers, config) {
					 $scope.selectIllegalList=response;// 违规情节
					 
					  if(opertionType=='Editor'){
						  var personSelectIllegal=$scope.person.illegal;
							if(personSelectIllegal){
								$.each($scope.selectIllegalList,function(index,item){
									  var attentionLevelId=item.attentionLevelId;
									  if(personSelectIllegal==attentionLevelId){
										  
										  initAttentionLevelList(attentionLevelId);
										  
										  return false;
									  }
								});
								
								
							}
							
						  
						  
					  }
				});
			
		}
		
		/**
		 * 违规情节下拉选择框改变事件
		 */
		$scope.selectIlleaglChangeFun=function(){
			
			  $scope.person.dateStart=null;
			  $scope.person.dateEnd=null;
			
			var personSelectIllegal=$scope.person.illegal;
			if(personSelectIllegal){
				$.each($scope.selectIllegalList,function(index,item){
					  var attentionLevelId=item.attentionLevelId;
					  if(personSelectIllegal==attentionLevelId){
						  
						  initAttentionLevelList(attentionLevelId);
						  
						  return false;
					  }
				});
			}
		}
		
		
		
		/**
		 * 获取违规情节对应的关注级别
		 */
		function initAttentionLevelList(attentionLevelId){
			 $http({
					method : 'post',
					cache : false,
					url : baseUrl + 'integrityBlack/initAttentionLevelList.do',
					params :{
						attentionLevelId:attentionLevelId
					}
				}).success(function(response, status, headers, config) {
					$scope.selectAttentionLevelList=response;// 关注级别
					
					if($scope.selectAttentionLevelList && $scope.selectAttentionLevelList.length>0){
						var obj=$scope.selectAttentionLevelList[0];
						var attentionMidId=obj.attentionMidId;
						$scope.person.attentionLevel=attentionMidId;
					}
					
					
					$scope.selectChangeFun();
				});
		}
		
		
	
		
		
		//身份证正则表达式
		var isIDCard2=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|[x|X])$/;
		var isIDCard1=/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9])$/;
		
		 //手机
        var moblie =/^1[3,5,7,8]\d{9}$/;
        //联系电话
        var phone = /^\d{3,4}-\d{7}$/;
		
		
		function initData(){
			 $http({
					method : 'post',
					cache : false,
					url : baseUrl + 'integrityBlack/initDataUser.do',
					params : {
					}
				}).success(function(response, status, headers, config) {
					$scope.opertionUser=response.message;
				});
		}
		
		//初始化函数
		initData();
		
		//社保号失去焦点后到后台查询数据填充表格数据
		$scope.onSociatioNumBlur=function(){
			var sociationNum=$scope.person.personCard;
			if(sociationNum)
			{
				
			initFormData(sociationNum);
				
			}
			
		}
		//动态根据社保号去后台获取数据填充表格数据
		function initFormData(sociationNum){
			var array=[];
			 $http({
					method : 'post',
					cache : false,
					url : baseUrl + 'integrityBlack/loadPatientBasicInfo.do',
					params : {
						personCard:sociationNum
					}
				}).success(function(response, status, headers, config) {
				        var status=response.status;
				         if(status){
				        	 array=response.result;
				        	 if(array.length>0){
				        		var obj=array[0];
				        		$scope.person.name=obj.name;
				    			$scope.person.gender=obj.gender;
				    			$scope.person.cardNo=obj.cardNo;
				    			$scope.person.cardType=obj.cardType;
				        	 }
				         }
				});
			
			
		}

        $scope.$watch('person.dateStart', function(){
            var dateStart = $scope.person.dateStart;
            if(null==dateStart || ""==dateStart){
                $scope.person.dateEnd=null;
            }else{
                if(!$scope.person.attentionLevel)
                {
                    Modal.alert("关注级别不能为空!");
                    $scope.person.dateStart=null;
                    return;
                }else{
                    var attentionLevel = $scope.person.attentionLevel;
                    
                	$.each($scope.selectAttentionLevelList,function(index,item){
  					  
                		var attentionMidId=item.attentionMidId;
                		
  					  if(attentionLevel==attentionMidId){
  						 var currentLevel=item.attentionLevel;
  						 
	  						 if(currentLevel=='A'){
	  	                        $scope.person.dateEnd="9999-01-01";
	  	                    }else{
	  	                        var dateStart2 = dateStart.substr(0,4);
	  	                        $scope.person.dateEnd=parseInt(dateStart2)+parseInt($scope.person.attentionTime)+dateStart.substr(4,dateStart.length);
	  	                    }
  					  }
  				});
                    
                   
                    
                    
                }
            }
        });
		
		$scope.onSubmit = function(){
			if(!$scope.person.personCard)
			{
				Modal.alert("社保号不能为空!");
				return;
			}
			if(!$scope.person.name)
			{
				Modal.alert("姓名不能为空!");
				return;
			}else if($scope.person.name.length>30){
				Modal.alert("姓名最长只能为30位");
				return;
			}
			
			if(!$scope.person.gender)
			{
				Modal.alert("性别不能为空!");
				return;
			}
			
			if($scope.person.inBlackReason){
				
				if($scope.person.inBlackReason.length>1000){
					Modal.alert("备注过长!");
					return;
				}
				
			}
			
			if(!$scope.person.happenDate)
			{
				Modal.alert("案发时间不能为空!");
				return;
			}
			
			if(!$scope.person.cardType)
			{
				Modal.alert("证件种类不能为空!");
				return;
			}
			
			
			
			if(!$scope.person.cardNo)
			{
				if($scope.person.cardNo==""){
					Modal.alert("证件号不能为空!");
					return;
				}else{
					Modal.alert("证件号格式不正确!");
					return;
				}
			}else if ($scope.person.cardType && $scope.person.cardType=='1'){ //身份证
				
				if(!isIDCard2.test($scope.person.cardNo)){
					Modal.alert("身份证号格式不对!");
					return;
				}
			}
			
			
			if(!$scope.person.phoneNumber && !$scope.person.mobileNumber)
			{
				
				Modal.alert("电话号和手机号码不能同时为空!");
				return;
			}else{
				
				
				if($scope.person.phoneNumber){
					if(!phone.test($scope.person.phoneNumber)){
						Modal.alert("请输入正确的电话号码,格式如000-0000000/0000-8888888!");
						return;
					}
					
				}else if($scope.person.mobileNumber){
					if(!moblie.test($scope.person.mobileNumber)){
						Modal.alert("请输入正确的11位手机号码!");
						return;
					}
				}
				
			}
			
			if(!$scope.person.illegal)
			{
				Modal.alert("违规情节不能为空!");
				return;
			}
			
			if(!$scope.person.attentionLevel)
			{
				Modal.alert("关注级别不能为空!");
				return;
			}
			
			
			
			    var startDate=$scope.person.dateStart;
	            var endDate=$scope.person.dateEnd;
				if(!startDate && !endDate)
				{
					Modal.alert("有效期不能为空!");
					return;
				}else{
					  if(!startDate){
						  Modal.alert("有效期开始日期不能为空!");
							return; 
					  }
					  if(!endDate){
						  Modal.alert("有效期结束日期不能为空!");
							return; 
					  }
					
					var start = new Date(startDate.replace(/-/g,"\/"));
					var end = new Date(endDate.replace(/-/g,"\/"));
					
					if(start>end){
						Modal.alert("开始有效期不能大于结束有效期");
						return;
					}
				}
			
			 $http({
					method : 'post',
					cache : false,
					url :baseUrl + 'integrityBlack/checkPatientBasicExist.do', //默认是保存的url,
					params :{
						personCard : $scope.person.personCard
					}
				}).success(function(response, status, headers, config) {
					var flag=response.status;
					var message=response.message;
					if(flag){
						saveProcess();
					}else{
					  Modal.alert("你输入的社保号不存在!");
					}
				});
		}
		
		
		function saveProcess(){
			var paramter={
					name : $scope.person.name,
					gender :$scope.person.gender,
					personCard : $scope.person.personCard,
					inBlackReason : $scope.person.inBlackReason,
					startDate : $scope.person.dateStart,
					endDate : $scope.person.dateEnd,
					cardNo : $scope.person.cardNo,
					cardType:$scope.person.cardType,
					happenDate:$scope.person.happenDate,
					pingy:$scope.person.pingy,
					phoneNumber:$scope.person.phoneNumber,
					mobileNumber:$scope.person.mobileNumber,
					attentionMidId:$scope.person.attentionLevel
				};
			
				if($scope.opertionType=='Editor'){
					paramter['id']=$scope.id;
				}
		    	 $http({
						method : 'post',
						cache : false,
						url : url,
						params : paramter
					}).success(function(response, status, headers, config) {
						var flag=response.status;
						var message=response.message;
						if(flag){
							$scope.person={};
							Modal.alert(message);
							saveAfterFun();
						}else{
							Modal.alert(message);
						}
					});
			
		}
		
		
        function saveAfterFun(){
			$scope.onClose();
			$scope.commonQuery();
			
		}
		
		//关闭model
		$scope.onClose = function() {
			$modalInstance.close();
		}

		
	}]);
})();